package dao

import (
	"fmt"
	"hft/Http/Models/request"

	"gorm.io/gorm"
)

type LouPanPhone struct{}

func (l *LouPanPhone) like(data request.LouPanPhoneLike, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("phone LIKE ? AND name LIKE ?", fmt.Sprintf("%%%v%%", data.Phone), fmt.Sprintf("%%%v%%", data.Name))
	}
}

func (l *LouPanPhone) is(data request.LouPanPhoneIs, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if data.Region == 0 {
			return db
		} else {
			return db.Where("region = ?", data.Region)
		}

	}
}

func (l *LouPanPhone) List(data request.LouPanPhonePage, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(l.like(data.LouPanPhoneLike, db)).Scopes(l.is(data.LouPanPhoneIs, db)).Scopes(Paginate(Page(data.Page), db))
	}
}

var LouPanPhoneDao = new(LouPanPhone)
